<template>
  <!-- 我是买家 待商家发货页面 -->
  <div>
     <!-- 搜索 -->
    <el-row :gutter="20">
      <el-col :span="12">
        <el-input
          placeholder="请输入需要查询的订单编号"
          v-model="ordernumber"
          clearable
          @clear="clearsearch()"
        >
          <template slot="prepend">按订单编号查询：</template>
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="ordernumberSearch()"
          ></el-button>
        </el-input>
      </el-col>
      <el-col :span="12">
        <el-input
          placeholder="请输入需要查询的内容"
          v-model="searchall"
          clearable
          @clear="clearsearch()"
        >
          <template slot="prepend">请输入要查询的内容：</template>
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="allsearch()"
          ></el-button>
        </el-input>
      </el-col>
    </el-row>
    <el-table :data="tableData1" border stripe>
      <el-table-column
        type="index"
        :index="indexNum"
        label="序号"
        width=""
        align="center"
      >
      </el-table-column>
      <el-table-column prop="order_no" label="订单编号" width="" align="center">
      </el-table-column>
      <el-table-column label="订单名称" width="200" align="left">
        <template slot-scope="scope">
          <div v-for="(item, index) in scope.row.books_info" :key="index">
            <img :src="api + item.info.image" alt="" />
            <span>{{ item.info.bookname }}x{{ item.number }}</span>
            <br />
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="nickname"
        label="卖家(昵称)"
        width=""
        align="center"
      >
        <template slot-scope="scope">
          <span v-for="(item, index) in scope.row.books_info" :key="index">
            {{ item.info.user_id }}
            <br />
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="createtime"
        label="下单时间"
        width=""
        align="center"
      >
      </el-table-column>
      <el-table-column label="收货人(昵称)" width="" align="center">
        <template slot-scope="scope">
          <span>
            {{ scope.row.order_address.name }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="订单金额" width="" align="center">
        <template slot-scope="scope">
          <span v-for="(item, index) in scope.row.books_info" :key="index">
            {{ item.total }}
            <br />
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="state" label="订单状态" width="" align="center">
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @next-click="handleNextClick"
      @prev-click="handlePrevClick"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="5"
      :current-page="currentpage"
      background
      layout="total,sizes, prev, pager, next, jumper"
      :total="tableData_length"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      api: this.imageapi,
      tableData: [],
      tableData1: [], //分页显示的数据
       ordernumber: "",
      searchall: "",
      currentpage: 1, //当前页面
      tableData_length: 0, //总条目数
      pageSize: 5, //默认每页显示10条
      user_id: sessionStorage.getItem("id"),
      orders_id: "" //生成的订单id
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    //element table自带序号
    indexNum(index) {
      return (this.currentpage - 1) * this.pageSize + index + 1;
    },
    //获取订单
    init() {
      const user_id = this.user_id;
      this.$http.post("/showdropshippingorder", { user_id }).then(res => {
        // console.log(res);
        this.tableData = res.data;
        this.tableData_length = res.data.length;
        this.handleCurrentChange(1);
        for (let i in this.tableData) {
          if (this.tableData[i].state == 0) {
            this.tableData[i].state = "待付款";
          } else if (this.tableData[i].state == 1) {
            this.tableData[i].state = "等待商家发货";
          } else if (this.tableData[i].state == 2) {
            this.tableData[i].state = "运输中";
          } else if (this.tableData[i].state == 3) {
            this.tableData[i].state = "待评价";
          } else {
            this.tableData[i].state = "已完成";
          }
        }
      });
    },
        //处理数据,分页
    handleTableData() {
      this.tableData1 = this.tableData.slice(
        (this.currentpage - 1) * this.pageSize,
        this.currentpage * this.pageSize
      );
    },
    // 每页显示多少条
    handleSizeChange(val) {
      this.pageSize = val;
      this.handleTableData();
    },
    //点击中间页的按钮 执行的方法
    handleCurrentChange(val) {
      this.currentpage = val;
      this.handleTableData();
    },
    //点击向前的按钮 执行的方法
    handlePrevClick(val) {
      this.currentpage = val;
      this.handleTableData();
    },
    //点击向后的按钮 执行的方法
    handleNextClick(val) {
      this.currentpage = val;
      this.handleTableData();
    },
    //清除搜索框
    clearsearch() {
      this.init();
    },
    //按订单编号查询
    ordernumberSearch() {
      if (this.ordernumber == "") {
        this.$message.warning("查询条件不能为空！");
        return;
      } else {
        let search = this.ordernumber;
        this.tableData = this.tableData.filter(item => {
          let searchField = { order_no: item.order_no };
          return Object.keys(searchField).some(key => {
            return (
              String(item[key])
                .toLowerCase()
                .indexOf(search) > -1
            );
          });
        });
      }
      this.tableData_length = this.tableData.length;
      this.handleCurrentChange(1);
    },
    //安全部查询过滤
    allsearch() {
      if (this.searchall == "") {
        this.$message.warning("查询条件不能为空！");
        return;
      } else if (this.searchall) {
        this.tableData = this.tableData.filter(item => {
          let res = JSON.stringify(item);
          if (res.indexOf(this.searchall) != -1) {
            return true;
          } else {
            return false;
          }
        });
      } else {
        this.tableData2 = this.tableData;
      }
      this.tableData_length = this.tableData.length;
      this.handleCurrentChange(1);
    }
  }
};
</script>

<style scoped>
img {
  height: 50px;
}
.el-input {
  margin-bottom: 10px;
}
.el-row{
  margin: 10px auto;
}
.el-pagination{
  text-align: center;
  margin-top: 10px;
}
</style>